package com.zry.yard.utils

import com.zry.yard.been.LongAndShortCode

/**
 * Created by zry on 2017/12/16.
 */
class StringListUtil {

    fun getString(stringList: MutableList<String>): String {
        var stringResult = ""
        for (i in stringList.indices) {
            stringResult += if (i == stringList.size - 1) {
                stringList[i]
            } else {
                stringList[i] + ","
            }
        }
        return stringResult
    }


    fun getList(stringResult: String): MutableList<String> {
        val stringList: MutableList<String> = mutableListOf()
        var strArray: Array<String>? = null
        strArray = stringResult.split(",".toRegex()).dropLastWhile { it.isEmpty() }.toTypedArray() // 拆分字符为"," ,然后把结果交给数组strArray
        for (i in strArray.indices) {
            stringList.add(strArray[i])
        }
        return stringList
    }


    fun getStringCode(stringList: MutableList<LongAndShortCode>): String {
        var stringResult = ""
        for (i in stringList.indices) {
            stringResult += if (i == stringList.size - 1) {
                stringList[i].longCode
            } else {
                stringList[i].longCode + ","
            }
        }
        return stringResult
    }


    /*
     *截取字符串前几位
     */
    fun getStartString(resultString: String, startString: Int): String {
        return resultString.substring(0, startString)
    }
    /*
     *截取字符串后几位
     */
    fun getEndString(resultString: String, endString: Int): String {
        return resultString.substring(resultString.length - endString, resultString.length)
    }
    /*
     *截取字符串中间几位
     */
    fun getCenterString(resultString: String, startString: Int, endString: Int): String {
        return resultString.substring(startString, endString)
    }
}
